package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import view.MainWindow;
import model.Carnet;
import model.Contact;
/**
 * the class is to set the actionlistener of the supprim button where is in a modify formulary
 * @author Estelle
 *
 */
public class SupprimAction implements ActionListener{
	private String name;
	private JFrame window = new JFrame();
	private JFrame form = new JFrame();
	
	public SupprimAction(JFrame frame, String name,JFrame mainframe){
		this.name = name;
		form = frame;
		window = mainframe;
	}
	
    @Override
    public void actionPerformed(ActionEvent event) {
        	FileDoc file = new FileDoc();
        	Carnet carnet = new Carnet("");
        	carnet.copy(file.recupCarnet("Estelle"));
        	Contact contact = new Contact();
        	int id = 0;
        	
        	for(Contact friend : carnet.getContent()){
				if(name.toLowerCase().equals(friend.getLastName().toLowerCase() + " " + friend.getFirstName().toLowerCase()) ){
					contact.copyContact(friend);
					break;
				}
			}
        	
        	if(!contact.emptyContact()){
        		
        		
        		id = carnet.getIDContact(contact);
        		
        		if (id>0) carnet.getContent().remove(id);
        		
        		carnet.getGroupContact().remove(contact.getGroupe());
        		carnet.updateGroupeAdd();
        		
        		file.resetCarnet(carnet);
        		file.orderCarnet(carnet);
        	
        		
        	}
        	else System.out.println(" ERROR l46 in controller.SupprimAction :contact was not found ");
        	
        	form.dispose();
        	window.dispose();
 
        	new MainWindow();
        	
        	
    }
  

}
